package com.hp.test.sys;
import com.alibaba.druid.support.json.JSONUtils;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hp.test.entity.SysUser;
import com.hp.test.p;
import javafx.scene.control.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import java.util.ArrayList;

import java.util.List;

@Service
public class SysUserService implements CommandLineRunner {
    @Resource
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysUserService1 sysUserService1;

    @Override
    public void run(String... args) throws Exception {
        try {
            p.p("======开始测试mybatisplus==sys_user有("+f()+")条数据=======");
            saveListTest();
            pinJieSqlFenYe();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //查询库里总共有几条数据
    public Integer f(){
        QueryWrapper<SysUser> queryWrapper=new QueryWrapper<SysUser>();
        Integer integer = sysUserMapper.selectCount(queryWrapper);
        return integer;
    }

    public void saveListTest(){
        List list=new ArrayList();
        SysUser s1=new SysUser();
        s1.setId(p.random16date());
        s1.setName("韩朋虎");
        s1.setBirthday(p.tod("1986-12-26"));
        SysUser s2=new SysUser();
        s2.setId(p.random16date());
        s2.setName("韩朋虎1");
        s2.setBirthday(p.tod("1986-12-27"));
        list.add(s1);list.add(s2);
        boolean b = sysUserService1.saveBatch(list);
        p.p("===saveListTest成功？==（"+b+"）======");
    }


    public void pinJieSqlFenYe(){
        //参数一是当前页，参数二是每页个数// 注意必须在config里注入 PaginationInterceptor才能用
        Page pag=new Page(2,10);
        String sql="select * from sys_user";
        List<SysUser> sysUsers = sysUserMapper.selectByPage(pag,sql);
        sysUsers.forEach(v->p.p(v.getName()));

    }



}
